package com.luocj.elephant.demo.view.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.luocj.elephant.demo.R;
import com.luocj.elephant.demo.utils.Utils;

public class HorProgressView extends View {

    private final int result;
    private int width = 0;
    private int height = 0;
    private int DEFAULT_COLOR = Color.GRAY;
    private int REACH_COLOR = Color.RED;
    private Paint paint;
    private Paint reachPaint;

    public HorProgressView(Context context) {
        this(context, null);
    }

    public HorProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HorProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        WIDTH = Utils.getScreenWidth(context);
//        HEIGHT = Utils.dip2px(context, 20);
        result = Utils.getScreenWidth(context);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Hor_Progress_View);
        ta.getDimension(R.styleable.Hor_Progress_View_HorWidth, width);
        ta.getDimension(R.styleable.Hor_Progress_View_HorHeight, height);
        ta.getColor(R.styleable.Hor_Progress_View_HorDefaultColor, DEFAULT_COLOR);
        ta.getColor(R.styleable.Hor_Progress_View_HorReachColor, REACH_COLOR);
        ta.recycle();


        initView();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            width = result;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            height = result;
        }


        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(DEFAULT_COLOR);
        Path path = new Path();
        path.moveTo(0, 0);
        path.lineTo(100, 0);
        path.lineTo(100, 100);
        path.lineTo(0, 100);
        path.close();
        canvas.drawPath(path, reachPaint);
    }

    private void initView() {
        paint = new Paint();
        paint.setColor(DEFAULT_COLOR);
        paint.setAntiAlias(true);
//        paint.setStrokeWidth();
        paint.setStyle(Paint.Style.FILL);

        reachPaint = new Paint();
        reachPaint.setColor(REACH_COLOR);
        reachPaint.setAntiAlias(true);
        reachPaint.setStyle(Paint.Style.FILL);
        reachPaint.setStrokeWidth(1);
    }
}
